<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="tags-app">
    <div class="container">
        <div class="tableBar">
            <el-input v-model="input" placeholder="请输入标签名称" style="width: 250px" clearable
                      @keyup.enter.native="handleQuery">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>
            <el-select v-model="tagType" placeholder="请选择标签类型" style="width: 150px;margin-left: 10px;" clearable>
                <el-option :label="item" :value="item" v-for="(item,index) in tagsTypeList">
                    {{item}}
                </el-option>
            </el-select>
            <el-button type="primary" icon="el-icon-search" @click="init" style="margin-left: 10px">搜索</el-button>
            <div class="tableLab">
                <el-button type="primary" @click="addTagstype('add')">+ 新建标签</el-button>
            </div>
        </div>
        <el-table :data="tableData" stripe class="tableBox" @selection-change="handleSelectionChange">
<!--            <el-table-column type="selection" width="25"></el-table-column>-->
            <el-table-column prop="name" label="标签名称" align="center"></el-table-column>
            <el-table-column prop="type" label="标签类型" align="center"></el-table-column>
            <el-table-column prop="createTime" label="创建时间" align="center">
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="blueBug" @click="addTagstype(scope.row.tagId)">修改
                    </el-button>
                    <el-button type="text" size="small" class="delBut non" @click="deleteHandle( scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                :current-page.sync="page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
</body>
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/tags.js"></script>
<script>
    new Vue({
        el: '#tags-app',
        data() {
            return {
                tableData: [{
                    tagId: '',
                    name: '',
                    type: ''
                }],
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tagType: "",
                tagsTypeList: []
            }
        },
        computed: {},
        created() {
            this.init()
            this.getTagTypes()
            // //根据标签类型获取标签名字
            // getTagListByType("会员卡").then(
            //     res=>{
            //         console.log(res)
            //     }
            // )
        },
        mounted() {
        },
        methods: {
            async init() {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize,
                    name: this.input ? this.input : undefined,
                    type: this.tagType ? this.tagType : undefined
                };
                await getTagsPage(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || [];
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            //获取标签类型
            getTagTypes() {
                getTagTypeList().then(
                    res => {
                        this.tagsTypeList = res.data;
                    }
                )
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },

            addTagstype(st) {
                if (st === 'add') {
                    window.parent.menuHandle({
                        id: '7',
                        url: 'page/tags/add.html',
                        name: '添加标签'
                    }, true)
                } else {
                    window.parent.menuHandle({
                        id: '7',
                        url: 'page/tags/update.html?id=' + st,
                        name: '修改标签'
                    }, true)
                }
            },
            // 全部操作
            handleSelectionChange(val) {
                let checkArr = [];
                val.forEach((n) => {
                    checkArr.push(n.id)
                });
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val;
                this.init()
            },
            deleteHandle(data) {
                console.log(data)
                this.$confirm('确认删除该标签, 是否继续?', '确定删除', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                }).then(() => {
                    deleteTags(data.tagId).then(res => {
                        console.log(res)
                        if (res.code === 1) {
                            this.$message.success('删除成功！');
                            this.handleQuery()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })


            },

        }
    })
</script>
</html>